home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / fflush.c < prev    next >
C/C++ Source or Header  |  1991-05-05  |  2KB  |  75 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "lib.h"
  6.  
  7. static int    _fflush    __PROTO((FILE *));
  8.  
  9. int fflush(fp)
  10.     register FILE *fp;
  11. /*
  12.  *    implementation note:  This function has the side effect of
  13.  *    re-aligning the virtual file pointer (in the buffer) with
  14.  *    the actual file pointer (in the file) and is therefore used
  15.  *    in other functions to accomplish this re-sync operation.
  16.  */
  17. {
  18.     register int f, i;
  19.  
  20.      if(fp)
  21.         return(_fflush(fp));
  22.     else
  23.         {
  24.         for(i=0; i<_NFILE; ++i)
  25.             {
  26.             f = _iob[i]._flag;
  27.             if(f & (_IOREAD | _IOWRT | _IORW))
  28.                 _fflush(&_iob[i]);
  29.             }
  30.         return(0);
  31.         }
  32. }
  33.  
  34. static int _fflush(fp)
  35.     register FILE *fp;
  36. {
  37.     register int f, rv = 0;
  38.     register long offset;
  39.  
  40.      if(fp == NULL)
  41.         return(0);
  42.     f = fp->_flag;
  43.       if (!(f & (_IORW | _IOREAD | _IOWRT)))  /* file not open */
  44.         return(0);
  45.     if(fp->_cnt > 0)             /* data in the buffer */
  46.         {
  47.           if(f & _IOWRT)                /* writing */
  48.               {
  49.             register long    todo;
  50.  
  51.             /* _cnt is cleared before writing to avoid */
  52.             /* loop if fflush is recursively called by */
  53.             /* exit if ^C is pressed during this write */
  54.             todo = fp->_cnt;
  55.             fp->_cnt = 0;
  56.             if(_write(fp->_file, fp->_base, todo) != todo) 
  57.                 {
  58.                 fp->_flag |= _IOERR;
  59.                 rv = EOF;
  60.                 }
  61.             }
  62.         else if(f & _IOREAD)             /* reading */
  63.             {
  64.             offset = -(fp->_cnt);
  65.             if(lseek(fp->_file, offset, 1) < 0)
  66.                 rv = EOF;
  67.             }
  68.         }
  69.     if(f & _IORW)
  70.         fp->_flag &= ~(_IOREAD | _IOWRT);
  71.     fp->_ptr = fp->_base;
  72.     fp->_cnt = 0;
  73.     return(rv);
  74. }
  75.